﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseUserTest.DB
{
    public class DBPlane
    {
        static DatabaseUserTest.DatabaseContext db = new DatabaseUserTest.DatabaseContext();

        public List<Plane> DepartureTime(DateTime departure)
        {
            List<Plane> planeList = new List<Plane>();
            var query = from p in db.Planes select p;

            foreach (Plane item in query)
            {
                double day = (departure - item.Departure).TotalDays;

                if (day == 0)
                {
                    planeList.Add(item);
                }
            }
            return planeList;
        }

        public List<Plane> ArrivalTime(DateTime arrival)
        {
            List<Plane> planeList = new List<Plane>();
            var query = from p in db.Planes select p;
            
            foreach (Plane item in query)
            {
                double day = (arrival - item.Arrival).TotalDays;

                if (day == 0)
                {
                    planeList.Add(item);
                }
            }
            return planeList;
        }

        public Plane FindPlane(int id)
        {
            Plane plane = db.Planes.Find(id);
            return plane;
        }

        public Plane FindPlane(DateTime departure, int routeId)
        {
            var query = from p in db.Planes select p;
            foreach (Plane item in query)
            {
                if ((departure <= item.Departure && item.Departure <= departure.AddHours(24)) && item.RouteId == routeId)
                {
                    return item;
                }
            }
            return null;
        }

        public List<Plane> FindAllPlanes()
        {
            List<Plane> planeList = new List<Plane>();
            var query = from p in db.Planes select p;

            foreach (Plane item in query)
            {
                planeList.Add(item);
            }
            return planeList;
        }

        public void AddPlane(Plane plane)
        {

            db.Planes.Add(plane);
            db.SaveChanges();
        }

        public void ReserveSeats(int check, List<Plane> planeList, int seats)
        {
            if (check == 0)
            {
                foreach (Plane item in planeList)
                {
                    db.Planes.Find(item.PlaneId).ReservedNormalSeats += seats;
                    db.SaveChanges();
                }
            }
            else
            {
                foreach (Plane item in planeList)
                {
                    db.Planes.Find(item.PlaneId).ReservedFirstClassSeats += seats;
                    db.SaveChanges();
                }
            }
        }

        public void UnreserveSeats(int check, List<Plane> planeList, int seats)
        {
            if (check == 0)
            {
                foreach (Plane item in planeList)
                {
                    db.Planes.Find(item.PlaneId).ReservedNormalSeats -= seats;
                    db.SaveChanges();
                }
            }
            else
            {
                foreach (Plane item in planeList)
                {
                    db.Planes.Find(item.PlaneId).ReservedFirstClassSeats -= seats;
                    db.SaveChanges();
                }
            }
        }



        //public void FillHerUp()
        //{
        //    foreach (var item in db.Planes)
        //    {
        //        DateTime arrivalTemp = item.Departure;
        //        string ligemeget = Convert.ToString(item.Route.Time);
        //        string[] ligemertsplit = ligemeget.Split(',');
        //        int hours = Convert.ToInt32(ligemertsplit[0]);

        //        arrivalTemp = arrivalTemp.AddHours(hours);

        //        if (ligemeget.Contains(",5"))
        //        {
        //            arrivalTemp = arrivalTemp.AddMinutes(30);
        //        }

        //        item.Arrival = arrivalTemp;
        //        db.SaveChanges();
        //    }
        //}

    }
}
